<?php

namespace common\models\sys;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "sys_setting".
 *
 * @property string $keyname
 * @property string $keyvalue
 */
class SysSetting extends \yii\db\ActiveRecord
{



    const MUTI = 1;
    const NOMUTI = 0;
    public static $ismuti = [
        self::MUTI => '多项',
        self::NOMUTI => '单项'
    ];



    const TYPE_FILE = 'file';
    const TYPE_INPUT = 'input';
    const TYPE_COLOR = 'color';
    public static $vtypes = [
        self::TYPE_INPUT => '输入框',
        self::TYPE_FILE => '文件上传',
        self::TYPE_COLOR => '颜色选择',
    ];


    const SHOW = 0;     //显示
    const HIDDEN =-1;       //不显示

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sys_setting';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['keyname'], 'required'],
            [['keyname','identify','viewtype'], 'string', 'max' => 30],
            [['ismuti','canuser'], 'integer'],
            [['keyvalue','help'], 'string', 'max' => 1000],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'keyname' => '设置项名称',
            'keyvalue' => '设置项值',
            'canuser' => '该设置项的管理人员'
        ];
    }


    public static function getValue($identify)
    {
        $result = (new Query())->select('keyvalue')->from(self::tableName())->where(['identify'=>$identify])->scalar();
        if(strpos($result,'||') !== false){
            return explode('||',$result);
        }
        return $result;
    }



    public static function setKeyValue($identify,$value)
    {
        SysSetting::updateAll(['keyvalue'=>$value],'identify=:identify',[':identify'=>$identify]);
    }
}
